package com.own.business.system.user.business.service.impl;

import com.own.business.login.core.entity.form.LoginForm;
import com.own.business.login.core.service.LoginService;
import com.own.business.system.account.business.service.SystemAccountService;
import com.own.business.system.account.common.em.AccountType;
import com.own.business.system.account.entity.SystemAccount;
import com.own.business.system.user.business.service.SystemRegisterService;
import com.own.component.common.exception.BusinessSimpleException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * SystemUserPhoneLoginServiceImpl
 *
 * @author chenxueli
 * @date 2023-06-25 22:59:00
 */
@Slf4j
@Service
public class SystemUserPhoneServiceImpl implements LoginService {

    @Resource
    private SystemAccountService systemAccountService;
    @Resource
    private SystemRegisterService systemRegisterService;

    /**
     * 用户账号密码登录
     *
     * @param form 登录表单
     * @return 登录的用户id信息
     */
    @Override
    public Long loginUser(LoginForm form) {
        var userId = systemAccountService.login(form.getAccount(), form.getPassword(), AccountType.TEXT, form.getType());
        if (userId != null) {
            return userId;
        }
        if (form.getRegister() == null || !form.getRegister()) {
            throw new BusinessSimpleException("账号或密码错误");
        }
        // 注册账号信息
        var vo = systemRegisterService.register(form.getAccount(), form.getPassword(), form.getClientId(), form.getType());
        return vo.getId();
    }

    /**
     * 获取账号信息
     *
     * @param content 账号信息
     * @return 账号信息
     */
    @Override
    public SystemAccount get(String content) {
        return null;
    }

}
